package cn.gz.demo.ui;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;

import androidx.annotation.RequiresApi;
import androidx.databinding.DataBindingUtil;

import cn.gz.demo.R;
import cn.gz.demo.SignBinding;
import cn.gz.demo.ui.base.BaseActivity;

/**
 * @author Liuyz
 * @date 2020/6/1511:22
 */
public class SignActivity extends BaseActivity {
    private SignBinding binding;
    private AnimationSet set;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_sign);

        // 创建平移和渐变的动画集合
        // 定义一个平移动画对象
        TranslateAnimation translate = new TranslateAnimation(0, 0, 0, -200);
        translate.setDuration(2000);
        //translate.setRepeatCount(1);

        // 渐变动画
        AlphaAnimation alpha = new AlphaAnimation(1, 0);
        alpha.setDuration(2000);
        alpha.setFillAfter(true);

        // 缩放动画
        ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.8f, 1, 0.8f);
        scaleAnimation.setDuration(2000);

        // 创建动画集合，将平移动画和渐变动画添加到集合中，一起start
        set = new AnimationSet(false);
        set.addAnimation(translate);
        set.addAnimation(alpha);
        set.addAnimation(scaleAnimation);
        // 隐藏部分布局的高度
        binding.sign.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View v) {
                binding.integral.startAnimation(set);
                set.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        binding.integral.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        binding.integral.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }
                });
            }
        });

        binding.nestedscrollview.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            private int totalDy = 0;

            @Override
            public void onScrollChanged() {
                totalDy = binding.nestedscrollview.getScrollY();
                if (totalDy > 40) {
                    binding.topIcon.setBackgroundResource(R.color.transparent);
                } else {
                    binding.topIcon.setBackgroundResource(R.mipmap.top);
                }
            }
        });
    }
}
